package com.cloudcross.ssp.loader;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.cloudcross.ssp.model.Advertiser;

/**
 * 将广告主代理商信息存入内存 创建人：wuqiang.du 创建时间：2016.01.04
 *
 */
public class AdvertiserLoader {
    private static Logger log = LogManager.getLogger(AdvertiserLoader.class);
    public static List<Advertiser> ADVERTISER_LIST = new ArrayList<Advertiser>();// 存放所有的广告主信息
    private static Map<Long, String> AGENTID_ADVERTISERID_MAP = new HashMap<Long, String>();// 存放agentId,advertiserId键值对

    /**
     * 获取所有的agentId放入set集合
     * 
     * @return
     */
    private static Set<Long> setAgentIdSet() {
        Set<Long> agentIdSet = new HashSet<Long>();
        if (ADVERTISER_LIST != null && ADVERTISER_LIST.size() > 0) {
            for (Advertiser adv : ADVERTISER_LIST) {
                agentIdSet.add(adv.getAgentId());
            }
        } else {
            log.debug("ADVERTISER_LIST为空，广告主信息为空或没有加载到内存，请检查！");
        }
        return agentIdSet;
    }

    /**
     * 处理加载内存中的广告主的数据,根据agentId把数据分成不同的组 key:agentId value:List<Advertiser>
     * list里面的数据有共同的agentId
     * 
     * @return
     */
    private static Map<Long, List<Advertiser>> setAgentIdKeyMap() {
        Map<Long, List<Advertiser>> agentKeyMap = new HashMap<Long, List<Advertiser>>();
        Set<Long> agentIdSet = setAgentIdSet();
        Iterator<Long> it = agentIdSet.iterator();
        Long agentId = null;
        List<Advertiser> advList = null;
        while (it.hasNext()) {// 外层循环，遍历set集合
            agentId = it.next();
            advList = new ArrayList<Advertiser>();
            Advertiser advertiser = null;

            Iterator<Advertiser> itr = ADVERTISER_LIST.iterator();
            while (itr.hasNext()) {// 内层循环，遍历ADVERTISER_LIST，删除已经匹配的广告主集合
                advertiser = itr.next();
                if (agentId.equals(advertiser.getAgentId())) {// 匹配代理商
                    advList.add(advertiser);// 将匹配代理商的广告主放到集合中
                    itr.remove();// 移除匹配好的广告主
                }
            }
            agentKeyMap.put(agentId, advList);
        }
        /*
         * for(Map.Entry<Long, List<Advertiser>> entry:agentKeyMap.entrySet()){
         * log.debug("key="+entry.getKey()+":value="+entry.getValue()); }
         */
        return agentKeyMap;
    }

    /**
     * 将通过agentId分类的数据广告主List集合中的广告主Id拼接成字符串，结果放到AGENTID_ADVERTISERID_MAP中
     * key:agenyId value:{advertiserId1,advertiserId2,advertiserId3,...}
     */
    public static void setAgentIdAdvertiserIdMap() {
        AGENTID_ADVERTISERID_MAP.clear();
        Map<Long, List<Advertiser>> map = setAgentIdKeyMap();
        List<Advertiser> list = null;
        for (Map.Entry<Long, List<Advertiser>> entry : map.entrySet()) {
            Long l = entry.getKey();
            list = entry.getValue();
            Integer count = list.size();
            String str = "";
            for (int i = 0; i < count - 1; i++) {
                str = str + list.get(i).getId() + ",";
            }
            str = str + list.get(count - 1).getId();
            AGENTID_ADVERTISERID_MAP.put(l, str);
        }
        /*
         * for(Map.Entry<Long,String>entry:AGENTID_ADVERTISERID_MAP.entrySet()){
         * log.debug("key="+entry.getKey()+":value="+entry.getValue()); }
         */
    }

    /**
     * 提供外部访问内存数据的方法
     * 
     * @param agentId
     * @return
     */
    public static String getAgentIdAdvertiserIdMap(Long agentId) {
        if (AGENTID_ADVERTISERID_MAP != null && AGENTID_ADVERTISERID_MAP.size() > 0) {
            return AGENTID_ADVERTISERID_MAP.get(agentId);
        } else {
            log.debug("无法从内存获取数据");
            return "";
        }
    }
}
